<?php
class user_detail_model extends CI_Model {
	var $fields = array('email','first_name', 'address1', 'address2', 'district_id', 'city_id', 'state_id', 'pincode','phone1','phone2','mobile1','mobile2', 'can_newsletter' );
	var $required_fields = array('email','first_name', 'mobile1');
	var $login_fields = array('my_email_id','pwd');
	var $forgot_fields = array('my_email_id');
	var $table_name = 'user_detail';
	var $user_id;
    function user_detail_model()
    {
        parent::__construct();
	
    }
    
    function getUsers($start = 0, $limit = 10,$where = array('deleted' => 0)){
    	$this->db->order_by('id DESC');
    	$this->db->where($where);
    	$query = $this->db->get($this->table_name,$limit,$start);
		$records = $query->result_array();

		$this->db->select('count(id) as total');
    	$this->db->where($where);
    	$rst = $this->db->get($this->table_name);
		$total = $rst->first_row()->total;
		
		$obj->records = $records;
		$obj->total = $total;
		return $obj;
    }
    
    
	function required($data = array()) {
    	if(empty($data)) return false;
    	$this->obj = get_instance();
    	$status = $this->obj->retrieve->required($this->required_fields, $data);
    	return $status;
    }
    
	function add($data = array(), $hash = ''){
    	if(empty($data)) return -1;
    	if($this->required($data) ) {
    		if($this->checkUniqueUsername($data['email'])) {	unset($data['mypassword']);
	    		$data['mypassword'] = md5($hash.$data['password']);
				$data['dob']        = $data['dobyy']."-".$data['dobmm']."-".$data['dobdd'];
				
	    		unset($data['dobyy'],$data['dobmm'],$data['dobdd'], $data['password'], $data['conf_password']);
				$obj->status = $this->db->insert($this->table_name, $data);
				$obj->user_id = $this->db->insert_id();
			
				//return $status;
				//$obj->student_id              = $this->student_id;
				return $obj;
    		} else 
    			return -3;
    	} else {
    		return -2;
    	}
    }
    
    function udpate($id = '', $data = array(), $hash =''){
    	if(empty($data) || empty($id)) return -1;

    	if($this->required($data)) {
    		$data['dob'] =$data['dobyy'].'-'.$data['dobmm'].'-'.$data['dobdd'];
			//if(!emty)
    		$data['mypassword'] = md5($hash.$data['password']);
			//echo $data['mypassword'];
    		unset($data['dobyy'],$data['dobmm'],$data['dobdd'], $data['password'], $data['conf_password']);
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			unset($data['mypassword']);
			return $status;
    	} else {
    		return -2;
    	}
    }
    
    function delete($id = ''){
		if(empty($id)) return -1;
		$data['deleted'] = 1;
		$this->db->where(array('id' => $id));
		$status = $this->db->update($this->table_name, $data);
		return $status;
    }
    
    function checkUniqueUsername($email = '') {
    	if(empty($email)) return false;
    	$this->db->where('email',$email);
    	$query = $this->db->get($this->table_name);
		$records = $query->result_array();
		if(empty($records)) {
			return true;
		} 
		return false;
    }
    
    function login($user = array(),$hash ='') {
    	if(empty($user) ) {
    		return false;
    	}
    	$where = array('email' => $user['my_email_id'], 'mypassword' => md5($hash.$user['pwd']));
    	$rst = $this->db->get_where($this->table_name, $where);
    	$records = $rst->row_array();
    	if(!empty($records)) {
    		$CI = & get_instance();
    		$my_sess = array();
    		$my_sess['email_id'] = $records['email'];
    		$my_sess['name'] = $records['first_name'];
    		$my_sess['user_id'] = $records['id'];
    		$CI->session->set_userdata('mylogin', $my_sess);
    		return true;
    	}
    	return false;
    }
    
	function changepwd($id = '', $data = array(), $hash=''){
    	if(empty($data) || empty($id)) return -1;
    	if(!empty($data['mypassword']))
    		$data['mypassword'] = md5($hash.$data['mypassword']);
    	else 
    		$data['mypassword'] = '';
    	if(!empty($data['mypassword']) || !empty($data['name'])) {
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
}
?>